/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tpdisenio2009.model.dao;

import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import tpdisenio2009.filters.AFAPlayerFilter;
import tpdisenio2009.model.HibernateUtils;
import tpdisenio2009.model.beans.RealTeam;
import tpdisenio2009.model.beans.SoccerPlayer;

/**
 *
 * @author Pablito
 */
public class SoccerPlayerDAO extends AbstractDAO<SoccerPlayer> {

    @Override
    public Class<SoccerPlayer> getPersistentClass() {
        return SoccerPlayer.class;
    }

    public List<SoccerPlayer> getSoccerPlayersByTeam(RealTeam realTeam ) {
        Session session = HibernateUtils.getCurrentSession();

        Criteria mainCriteria = session.createCriteria( SoccerPlayer.class ).createAlias( "realTeam", "rt" );
        mainCriteria.add( Restrictions.eq( "rt.name", realTeam.getName() ) );

        return (List<SoccerPlayer>) mainCriteria.list();
    }

    public List<SoccerPlayer> getByPosition( String position ) {
        Session session = HibernateUtils.getCurrentSession();

        Criteria mainCriteria = session.createCriteria( SoccerPlayer.class );
        mainCriteria.add( Restrictions.eq( "position" , position) );

        return mainCriteria.list();
    }

    public List<SoccerPlayer> getByFilter( AFAPlayerFilter soccerPlayerFilter ){
        Session session = HibernateUtils.getCurrentSession();

        Criteria mainCriteria = session.createCriteria( SoccerPlayer.class ).createAlias( "realTeam", "rt" );

        if ( soccerPlayerFilter.getClub() != null ) {
            mainCriteria.add( Restrictions.eq( "rt.name", soccerPlayerFilter.getClub().getName() ) ); }

        if ( soccerPlayerFilter.getPosicion() != null ) {
            mainCriteria.add( Restrictions.eq( "position" , soccerPlayerFilter.getPosicion() ) ); }

        // Restrictions.ge equivale a >=
        if ( soccerPlayerFilter.getMinCotization() != null ) {
            mainCriteria.add( Restrictions.ge( "price" , soccerPlayerFilter.getMinCotization() ) ); }

        // Restrictions.le equivale a <=
        if ( soccerPlayerFilter.getMaxCotization() != null ) {
            mainCriteria.add( Restrictions.le( "price" , soccerPlayerFilter.getMaxCotization() ) ); }

        return (List<SoccerPlayer>) mainCriteria.list();
    }

}
